package Formularios;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Negocio.negAviones;
import Objetos.Aviones;

public class frmAltaAvion extends JFrame {
	private JPanel panel;
	private JButton btnAceptar;
	private JButton btnCancelar;
	private JLabel lblAvion;
	private JLabel lblCapacidad;
	private JTextField txtAvion;
	private negAviones negAvion = new negAviones();
	private JTextField txtCapacidad;

	
	public frmAltaAvion() {
		setTitle("Alta Aeropuerto");		
		setBounds(300, 20, 400, 200);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);	 
	    panel= new JPanel();
	    getContentPane().add(panel);
	    panel.setLayout(null);
	
	    lblAvion = new JLabel("Avion:");
	    lblAvion.setBounds(50, 20, 50, 30);
	    
	    lblCapacidad = new JLabel("Capacidad:");
	    lblCapacidad.setBounds(20, 70, 80, 30);
	    
	    
	    btnAceptar = new JButton("Aceptar");
		btnAceptar.setBounds(220, 120, 100, 30);
			
		btnCancelar = new JButton("Cancelar");
		btnCancelar.setBounds(90, 120, 100, 30);
		
		
		txtAvion = new JTextField();
		txtAvion.setBounds(90, 20, 150, 30);
	 	
		txtCapacidad= new JTextField();
		txtCapacidad.setBounds(90, 70, 80, 30);
			
		
		onAceptarClick();	
	    panel.add(btnAceptar);
	    panel.add(txtAvion);	    
	    panel.add(lblAvion);
	    panel.add(txtCapacidad);
	    panel.add(lblCapacidad);
	    panel.add(btnCancelar);
	}

	private void onAceptarClick() {
		btnAceptar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			try{
				if((! txtCapacidad.getText().equals("")) ||  (! txtAvion.getText().equals(""))){
				
					negAvion.insertarAvion(new Aviones(1,Integer.parseInt(txtCapacidad.getText()), txtAvion.getText() ));
					
					JOptionPane.showMessageDialog(null,"Avion guardado.");
					setVisible(false);
				}else {
					JOptionPane.showMessageDialog(null,"Debe completar los campos");
				}				
			}catch(Exception i){
				JOptionPane.showMessageDialog(null,i.getMessage());
			}			
		}
			
		});
		btnCancelar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {								
				setVisible(false);			
			}
		});
	}
	
	
}
